package com.cnc.ssm.web;

import com.cnc.bas.util.Constants;
import com.cnc.bas.util.LTUtil;
import com.cnc.bas.web.BasMapForm;
import com.cnc.bas.web.SecureBaseDispatchAction;
import com.cnc.ssm.ISsmService;
import com.cnc.ssm.model.PwCodeInfo;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.util.RequestUtils;
 

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
/**
 * User: Du bf
 * Date: 2004-11-29
 */

public class CodeInfoAction extends SecureBaseDispatchAction {
    private ISsmService ssmService = null;

    public void setServlet(ActionServlet actionServlet) {
        super.setServlet(actionServlet);
        ssmService = (ISsmService) this.getBean(Constants.BEAN_NAME_SSM);
    }

    public ActionForward list(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HashMap hs = ((BasMapForm) form).getHs();
        List list = ssmService.listCodeInfo(hs);
        LTUtil.setPageData(request, list);
        return mapping.findForward("list");
    }

    public ActionForward create(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        return mapping.findForward("edit");
    }

    public ActionForward add(ActionMapping mapping,
                             ActionForm form,
                             HttpServletRequest request,
                             HttpServletResponse response) throws Exception {
        ssmService.addCodeInfo(((CodeInfoForm) form).getCodeInfo());
        return mapping.findForward("edit");
    }

    public ActionForward edit(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
    	
    	String code = request.getParameter("id");
        long codeId = Long.parseLong(code);
        ((CodeInfoForm) form).setCodeInfo((PwCodeInfo) (ssmService.loadCodeInfo(new Long(codeId))));
        return mapping.findForward("edit");
    }

    public ActionForward update(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        ssmService.updateCodeInfo(((CodeInfoForm) form).getCodeInfo());
        return mapping.findForward("edit");
    }

    public ActionForward delete(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        String code = request.getParameter("id");
        long codeId = Long.parseLong(code);
        ssmService.deleteCodeInfo(codeId);
        return mapping.findForward("list");
    }
}
